package com.yuntongxun.ecdemo.voicechat.ui.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.flyco.systembar.SystemBarHelper;
import com.umeng.analytics.MobclickAgent;
import com.yuntongxun.ecdemo.R;
import com.yuntongxun.ecdemo.common.utils.CommomUtil;
import com.yuntongxun.ecdemo.voicechat.ui.BaseUi;
import com.yuntongxun.ecdemo.voicechat.util.CommonUtils;

import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;

public abstract class BaseChatActivity extends Activity implements BaseUi {

    /**
     * context
     */
    protected Context mContext = null;

    protected Toolbar mToolbar;

    protected TextView mTitleTv;

    protected ImageView mBackBtn;

    protected TextView mRightBtnTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mContext = this;

        setupSystemBar();

        if (isBindEventBusHere()) {
            EventBus.getDefault().register(this);
        }

        if (getContentViewLayoutID() != 0) {
            setContentView(getContentViewLayoutID());
        } else {
            throw new IllegalArgumentException("You must return a right contentView layout resource Id");
        }

        initViewsAndEvents();
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        super.setContentView(layoutResID);

        ButterKnife.inject(this);

        mToolbar = ButterKnife.findById(this, R.id.common_toolbar);
        if (null != mToolbar) {

        }

        mTitleTv = ButterKnife.findById(this, R.id.toolbar_title);

        mBackBtn = ButterKnife.findById(this, R.id.toolbar_back_btn);

        mRightBtnTv = ButterKnife.findById(this, R.id.toolbar_right_title);

        if(mBackBtn != null){
            mBackBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onBackPressed();
                }
            });
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        ButterKnife.reset(this);

        if(isBindEventBusHere()){
            EventBus.getDefault().unregister(this);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();

        MobclickAgent.onPause(this);
    }

    private void setupSystemBar(){

        SystemBarHelper.tintStatusBar(this, getResources().getColor(R.color.colorPrimaryDark));

    }

    /**
     * 设置标题
     * @param title
     */
    protected void setTitle(String title){
        if(mTitleTv != null){
            mTitleTv.setText(title);
        }
    }

    /**
     * 设置右上角按钮内容
     * @param rightTitle
     */
    protected void setRightTitle(String rightTitle){

        if(mRightBtnTv != null){
            mRightBtnTv.setText(rightTitle);
        }
    }

    /**
     * 点击右边按钮
     */
    public void clickRightTv(){

    }

    /**
     * 是否绑定eventBus
     */
    protected abstract boolean isBindEventBusHere();

    /**
     * 绑定布局xml文件
     */
    protected abstract int getContentViewLayoutID();

    /**
     * 初始化布局和事件，在onFirstUserVisible之前执行
     */
    protected abstract void initViewsAndEvents();

    @Override
    public void showToastLong(String msg) {
        if (null != msg && !CommonUtils.isEmpty(msg)) {
            toast(msg, Toast.LENGTH_LONG);
        }
    }

    @Override
    public void showToastShort(String msg) {
        if (null != msg && !CommonUtils.isEmpty(msg)) {
            toast(msg, Toast.LENGTH_SHORT);
        }
    }

    private Toast mToast = null;
    private void toast(String msg, int duration){
        if (mToast == null) {
            mToast = Toast.makeText(mContext, msg, duration);
        } else {
            mToast.setText(msg);
            mToast.setDuration(duration);
        }
        mToast.show();
    }

    ProgressDialog progressDialog = null;

    @Override
    public void showKProgress(String label) {

        progressDialog = new ProgressDialog(mContext);
        progressDialog.setMessage(label);

        progressDialog.show();

    }

    @Override
    public void showKProgress(String label, boolean isCancelable) {
        progressDialog = new ProgressDialog(mContext);
        progressDialog.setMessage(label);

        progressDialog.setCancelable(isCancelable);
        progressDialog.show();

    }

    @Override
    public void dimissKProgress() {

        if(progressDialog != null){
            progressDialog.dismiss();
        }
    }
}
